Thread: "Abort trap" error in c++

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    "Abort trap" error in c++

    I have an array of size 8, and I send only the second half of the array to my insertion sort. It successfully sorts only the second half of the array, but there is a statement at the bottom, "Abort trap: 6". Below are my main.cc and my insertionSort.cc files. What is causing the "Abort trap: 6" error?




    Here is the output:
    Before sorting
    9 8 10 2 4 6 50 1
    After sorting
    9 8 10 2 1 4 6 50
    Abort trap: 6




    main.cc
    Code:
    #include <cstdio>
    #include "insertionSort.h"
    
    
    int main() {
    
    
        // create array
        int array[] = {9, 8, 10, 2, 4, 6, 50, 1};
    
    
        // get size of the array
        int size = sizeof(array) / sizeof(array[0]);
    
    
        // display array before reversing
        printf("Before sorting\n");
        for (int i = 0; i < size; i++) {
            printf("%d ", array[i]);
        } 
    
    
        // new line
        printf("\n");
    
    
        // sort only the second half of the array
        insertionSort(array+(size/2), size);
    
    
        // display the array again after reversing
        printf("After sorting\n");
        for (int i = 0;i < size; i++) {
            printf("%d ", array[i]);
    
    
        } // end of for loop
    
    
        // new line
        printf("\n");
    
    } // end of main




    insertionSort.cc
    Code:
    #include "insertionSort.h"
    
    
    void insertionSort(int* array, int size) {
    
    
        for (int i = 1; i < size; i++) {
            int key = array[i];
            int j = i - 1;
    
    
            while (j >= 0 && array[j] > key) {
                array[j+1] = array[j];
                j--;
            } // end of while loop 
    
    
            // swap
            array[j+1] = key;
    
    
            } // end of for loop
    
    
    } // end of insertionSort

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    At a glance:

    Code:
    insertionSort(array+(size/2), size);
    If you're passing only the second half of the array, then the second argument (size) should be adjusted accordingly so you don't exceed the bounds of the array.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abort Trap Error
    By DrSeptapus in forum C Programming
    Replies: 2
    Last Post: 12-07-2010, 11:06 AM
  2. Balloon tip question: Trap when the user clicks on "X"
    By Joelito in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2005, 10:35 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread